package handler

import (
	"design-hub/model"
	"design-hub/pkg"
	"github.com/gin-gonic/gin"
)

func Add(c *gin.Context) {
	tel := c.PostForm("tel")
	password := c.PostForm("password")
	var user model.User
	err := model.DB.Where("tel = ? and password = ?", tel, password).First(&user).Error
	if err == nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不能重复",
		})
		return
	}
	if len(tel) != 11 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "手机号不是11为",
		})
		return
	}
	data := model.User{
		Tel:      tel,
		Password: pkg.Md5str1(password),
	}
	err = model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}
}
